<template>
  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input v-model="data.username" prefix-icon="Search" style="width: 240px; margin-right: 10px" placeholder="请输入用户昵称"></el-input>
      <el-input v-model="data.title" prefix-icon="Search" style="width: 240px; margin-right: 10px" placeholder="请输入帖子标题"></el-input>
      <el-button type="info" plain @click="load">查询</el-button>
      <el-button type="warning" plain style="margin: 0 10px" @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-button type="danger" plain @click="handleBatchDelete">批量删除</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-table stripe :data="data.tableData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="image" label="帖子封面">
          <template #default="scope">
            <el-image style="width: 50px; height: 50px; border-radius: 5px" :src="scope.row.image" :preview-src-list="[scope.row.image]" preview-teleported></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="用户昵称"></el-table-column>
        <el-table-column prop="title" label="帖子标题"></el-table-column>
        <el-table-column prop="content" label="帖子内容">
          <template #default="scope">
            <el-button type="primary" @click="view(scope.row.content)">查看内容</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发布时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="viewCount" label="浏览量"></el-table-column>
        <el-table-column prop="status" label="审核状态">
          <template v-slot="scope">
            <el-tag type="warning" v-if="scope.row.status === '待审核'">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.status === '审核通过'">审核通过</el-tag>
            <el-tag type="danger" v-if="scope.row.status === '审核拒绝'">审核拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template v-slot="scope">
            <el-button type="success" plain
                       v-if="scope.row.status === '待审核'"
                       @click="audit(scope.row, '审核通过')">
              通过
            </el-button>
            <el-button type="danger" plain
                       v-if="scope.row.status === '待审核'"
                       @click="audit(scope.row, '审核拒绝')"
            >
              拒绝
            </el-button>
            <el-button type="danger" circle :icon="Delete" @click="handleDelete(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="card" v-if="data.total">
      <el-pagination
          @current-change="load"
          @size-change="handleSizeChange"
          background layout="total, prev, pager, next, jumper, sizes"
          :page-sizes="[1, 3, 5, 10, 15, 20, 30]"
          :page-size="data.pageSize"
          v-model:current-page="data.pageNum"
          :total="data.total"/>
    </div>

    <!--查看内容-->
    <el-dialog title="内容" v-model="data.viewVisible" width="50%" :close-on-click-modal="false" destroy-on-close>
      <div class="editor-content-view" style="padding: 20px" v-html="data.content"></div>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="data.viewVisible = false">关 闭</el-button>
    </span>
      </template>
    </el-dialog>

  </div>
</template>
<script setup>
  import {reactive} from 'vue'
  import request from '@/utils/request.js'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { Delete, Edit} from '@element-plus/icons-vue'

  const data = reactive({
    pageNum: 1,
    pageSize: 10,
    username: null,
    title: null,
    tableData: [],
    total: 0,
    ids: [],
    content: null,
    viewVisible: null
  })

  const load = () => {
    request.post('/post/pageQuery', {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      userName: data.username,
      title: data.title
    }).then(res => {
      if (res.code === '200') {
        data.tableData = res.data?.list || []
        data.total = res.data?.total
      } else {
        ElMessage.error(res.msg)
      }
    })
  }

  load()

  const reset = () => {
    data.username = null
    data.title = null
    load()
  }

  const handleSizeChange = (newSize) => {
    data.pageSize = newSize
    data.pageNum = 1
    load()
  }

  const view = (content) => {
    data.content = content
    data.viewVisible = true
  }

  // 审核
  const audit = (row, status) => {
    ElMessageBox.confirm('您确定吗？', '确认', {type: 'warning'}).then(res => {
      request.put('/post/audit', {
        id: row.id,
        status: status
      }).then(res => {
        if (res.code === '200') {
          ElMessage.success('操作成功')
          load()
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch()
  }

  const handleDelete = (id) => {
    ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗？', '删除确认', { type: 'warning' }).then(res => {
      request.delete('/post/delete/' + id).then(res => {
        if (res.code === '200') {
          ElMessage.success('删除成功')
          load()
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
      console.log(err)
    })
  }

  const handleSelectionChange = (rows) => {
    data.ids = rows.map(row => row.id)
  }

  const handleBatchDelete = () => {
    if (!data.ids.length) {
      ElMessage.warning('请选择数据')
      return
    }
    ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗？', '删除确认', { type: 'warning' }).then(res => {
      request.delete('/post/delete/batch', {data: data.ids} ).then(res => {
        if (res.code === '200') {
          ElMessage.success('批量删除成功')
          load()
        } else {
          ElMessage.error(res.msg)
        }
      })
    })
  }

</script>